package com.zu.commu.insertDataServletXml.localOperate;

import org.apache.log4j.Logger;

import java.util.Vector;

public class SaveLocal {
	private static SaveLocal instance;
	private static Vector<String> ve = new Vector();
	private Object synObj = new Object();
	private static SaveLocalThread slThread = null;

	public static SaveLocal instance() {
		if (instance == null) {
			instance = new SaveLocal();
			slThread = new SaveLocalThread();
			slThread.start();
		}
		return instance;
	}

	public void setXmlData(String xml) {
		synchronized (this.synObj) {
			ve.add(xml);
			this.synObj.notifyAll();
		}
	}

	private String getXmlData() {
		String xml = null;
		synchronized (this.synObj) {
			while (ve.size() == 0)
				try {
					this.synObj.wait();
				} catch (Exception localException) {
				}
		}
		xml = (String) ve.get(0);
		ve.remove(0);
		return xml;
	}

	private static class SaveLocalThread extends Thread {
		private static Logger log = Logger
				.getLogger("com.automic.commu.insertDataServletXml.localOperate.SaveLocal");

		public void run() {
			String xml = null;
			while (true) {
				xml = SaveLocal.instance.getXmlData();
				XmlOperator.instance().saveXmlData(xml);
				log.info("本地保存了一个数据");
			}
		}
	}
}
